﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Drawing;
using System.Diagnostics;

namespace MyEngineExercise.我的扩展
{
    /// <summary>
    /// Summary description for OpenNewMapDocument.
    /// </summary>
    [Guid("5bf50443-f852-47cd-9c96-984184b6cca6")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MapAndPageLayoutSynchApp.OpenNewMapDocument")]
    public sealed class OpenNewMapDocument : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion


        /// <summary>
        /// IHookHelper实例
        /// </summary>
        private IHookHelper _hookHelper;
        /// <summary>
        /// 控制map和page控件
        /// </summary>
        private MapAndPageSync _mapAndPageSync = null;


        /// <summary>
        /// mxd文档路径
        /// </summary>
        private string _docPath = string.Empty;
        /// <summary>
        /// mxd文档路径的属性
        /// </summary>
        public string DocumentFileName
        {
            get { return _docPath; }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapAndPageSync"></param>
        public OpenNewMapDocument(MapAndPageSync mapAndPageSync)
        {
            base.m_category = ".NET Samples";
            base.m_caption = "Open Map Document";
            base.m_message = "Open Map Document";
            base.m_toolTip = "Open Map Document";
            base.m_name = "DotNetSamplesOpenMapDocument";

            _mapAndPageSync = mapAndPageSync;

            try
            {
                string bitmapResourceName = GetType().Name + ".bmp";
                //获取资源
                System.Drawing.Bitmap icon = global::MyEngineExercise.Properties.Resources.OpenNewMapDocument;
                base.m_bitmap = icon;
            }
            catch(Exception ex)
            {
                Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }


        #region Overriden Class Methods
        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (null == hook)
                return;
            if (null == _hookHelper)
                _hookHelper = new HookHelperClass();
            _hookHelper.Hook = hook;
        }

        public override void OnClick()
        {
            //launch a new OpenFile dialog
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Map Documents (*.mxd)|*.mxd";
            dlg.Multiselect = false;
            dlg.Title = "Open Map Document";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string docName = dlg.FileName;
                IMapDocument mapDoc = new MapDocumentClass();
                //如果是当前文档 且 没有密码保护
                if (mapDoc.get_IsPresent(docName) && !mapDoc.get_IsPasswordProtected(docName))
                {
                    mapDoc.Open(docName,string.Empty);//打开
                    _docPath = docName;//保存mxd文档路径

                    // set the first map as the active view，打开文档的第一个map
                    IMap map = mapDoc.get_Map(0);
                    mapDoc.SetActiveView((IActiveView)map);//设置activeview

                    //设置page控件的PageLayout是mapDoc.PageLayout
                    _mapAndPageSync.PageLayoutControl.PageLayout = mapDoc.PageLayout;
                    _mapAndPageSync.ReplaceMap(map);

                    mapDoc.Close();

                }
            }
        }
        #endregion
    }
}
